package com.gxuwz.eschool;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


public class NewsActivity extends AppCompatActivity {
    //定义点击返回按钮
    private Button btn_back;
    private RecyclerView mRecyclerView;
    private HomeAdapter mAdapter;
    //需要的适配器
    private String[] names = {"北校区送电通知！","水费、电费注意！","公文注意！","喝水注意！","胖猫注意！","吃饭注意！"};
    private String[] Time = {"2022-9-12 12:34","2022-9-12 12:34","2022-9-12 12:34","2022-9-12 12:34","2022-9-12 12:34","2022-9-12 12:34"};
    private String[] content = {"来源：一卡通技术","来源：筑波技术","来源：一卡通技术","来源：筑波技术","来源：一卡通技术","来源：筑波技术"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news); // 使用你的首页布局
        //去除默认标题栏
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }
        mRecyclerView = (RecyclerView) findViewById(R.id.lv_list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //创建一个适配器实例
        mAdapter = new HomeAdapter();
        //将适配器添加到列表控件上
        mRecyclerView.setAdapter(mAdapter);

        //方法区
        fanhui();

    }
    public void fanhui() {
        btn_back =  findViewById(R.id.btn_back);
        //返回首页,物理返回就行了
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();//默认的返回类型，等同于按下了物理返回键
            }
        });

    }

    //适配器等其他
    class HomeAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(NewsActivity.this).inflate(R.layout.news_list,null);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.News_name.setText(names[position]);
            holder.News_Time.setText(Time[position]);
            holder.News_from.setText(content[position]);
        }

        @Override
        public int getItemCount() {
            return names.length;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView News_name,News_Time,News_from;
        //初始化linearLayout
        RelativeLayout relativeLayout;
        public MyViewHolder(View view) {
            super(view);
            News_name = view.findViewById(R.id.News_name);
            News_Time = view.findViewById(R.id.News_Time);
            News_from = view.findViewById(R.id.News_from);

            //在这里编写让每一个表单都能触发新闻公告事件
            //获取news_list里的id
            relativeLayout = itemView.findViewById(R.id.Line_all);
            // 设置点击事件监听器
            relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view1) {
                    Intent intent = new Intent(view1.getContext(), NewsContentActivity.class);
                    view1.getContext().startActivity(intent);
                }
            });
        }
    }

}
